package com.zhuangfei.hpuhelper.view;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhuangfei.hpuhelper.adapter.MyFragmentPagerAdapter;
import com.zhuangfei.hpuhelper.fragment.BodyScoreFragment;
import com.zhuangfei.hpuhelper.fragment.FailScoreFragment;
import com.zhuangfei.hpuhelper.fragment.HistoryScoreFragment;
import com.zhuangfei.hpuhelper.fragment.ScoreFragment;
import com.zhuangfei.hpuhelper.tools.DimensionTools;
import com.zhuangfei.toolkit.model.BundleModel;
import com.zhuangfei.toolkit.tools.ActivityTools;
import com.zhuangfei.toolkit.tools.BundleTools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.lzf.serviceplatform.R;

public class ScoreTotalActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    //适配器
    private MyFragmentPagerAdapter mAdapter;
    private ViewPager viewPager;
    private Unbinder unbinder;

    private int screenWidth;

    @BindView(R.id.id_score_total_toolbar1)
    TextView finalScoreTextView;

    @BindView(R.id.id_score_total_toolbar2)
    TextView historyScoreTextView;

    @BindView(R.id.id_score_total_toolbar3)
    TextView failScoreTextView;

    @BindView(R.id.id_score_total_toolbar4)
    TextView bodyScoreTextView;


    @BindView(R.id.id_score_total_lineTextView)
    LinearLayout lineTextView;

    LinearLayout.LayoutParams lp;

    Class<?> returnClass=null;

    int toItem=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_total);
        ActivityTools.setTransparent(this);
        unbinder = ButterKnife.bind(this);
        initBundle();
        initView();
    }

    private void initBundle() {
        returnClass= BundleTools.getFromClass(this,MenuActivity.class);
        toItem=BundleTools.getToItem(this,0);
    }

    private void initView() {
        List<Fragment> fragments=new ArrayList<>();
        ScoreFragment finalScoreFragment=new ScoreFragment();
        HistoryScoreFragment historyScoreFragment=new HistoryScoreFragment();
        FailScoreFragment failScoreFragment=new FailScoreFragment();
        BodyScoreFragment bodyScoreFragment=new BodyScoreFragment();

        fragments.add(finalScoreFragment);
        fragments.add(historyScoreFragment);
        fragments.add(failScoreFragment);
        fragments.add(bodyScoreFragment);

        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments);
        viewPager = (ViewPager) findViewById(R.id.id_score_total_viewpager);
        viewPager.setAdapter(mAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(this);

        screenWidth= getResources().getDisplayMetrics().widthPixels;
        lp=new LinearLayout.LayoutParams(screenWidth/4, DimensionTools.dip2px(this,2));
        lineTextView.setLayoutParams(lp);
        select(toItem);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        Log.i("OnPageScrolled","pos:"+position+","+positionOffset+","+positionOffsetPixels);
        float f=position+positionOffset;
        lp.setMargins((int)((screenWidth/4)*f),0,0,0);
        lineTextView.setLayoutParams(lp);
    }

    @Override
    public void onPageSelected(int position) {
        select(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @OnClick({R.id.id_score_total_toolbar1, R.id.id_score_total_toolbar2, R.id.id_score_total_toolbar3, R.id.id_score_total_toolbar4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_score_total_toolbar1:
                select(0);
                break;
            case R.id.id_score_total_toolbar2:
                select(1);
                break;
            case R.id.id_score_total_toolbar3:
                select(2);
                break;
            case R.id.id_score_total_toolbar4:
                select(3);
                break;
            default:
                break;
        }
    }

    public void initTab(){
        int color=getResources().getColor(R.color.app_gray);
        finalScoreTextView.setTextColor(color);
        historyScoreTextView.setTextColor(color);
        failScoreTextView.setTextColor(color);
        bodyScoreTextView.setTextColor(color);
    }

    public void select(int i) {
        initTab();
        int color=getResources().getColor(R.color.theme_red);

        switch (i) {
            case 0:
                viewPager.setCurrentItem(0);
                finalScoreTextView.setTextColor(color);
                break;
            case 1:
                viewPager.setCurrentItem(1);
                historyScoreTextView.setTextColor(color);
                break;
            case 2:
                viewPager.setCurrentItem(2);
                failScoreTextView.setTextColor(color);
                break;
            case 3:
                viewPager.setCurrentItem(3);
                bodyScoreTextView.setTextColor(color);
                break;
            default:
                break;
        }
    }

    @OnClick(R.id.id_back)
    public  void goBack(){
        ActivityTools.backActivity(this, returnClass);
    }

    @Override
    public void onBackPressed() {
        goBack();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
